package com.xindacloud.sms.console.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.console.result.Result;
import com.xindacloud.sms.console.result.ResultGenerator;
import com.xindacloud.sms.console.system.dto.SmsDashBoardReportDTO;
import com.xindacloud.sms.console.system.dto.SmsSubmissionRecordDTO;
import com.xindacloud.sms.console.system.dto.SmsTotalDashBoardReportDTO;
import com.xindacloud.sms.console.system.entity.SmsSubmissionRecord;
import com.xindacloud.sms.console.system.entity.SmsUser;
import com.xindacloud.sms.console.system.mapper.SmsSubmissionRecordMapper;
import com.xindacloud.sms.console.system.mapper.SmsUserMapper;
import com.xindacloud.sms.console.system.service.ISmsSubmissionRecordService;
import com.xindacloud.sms.console.system.service.ISmsUserAllowOperatorService;
import com.xindacloud.sms.console.system.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 短信发送记录 服务实现类
 * </p>
 *
 * @author czc
 * @since 2019-10-15
 */
@Service
public class SmsSubmissionRecordServiceImpl extends ServiceImpl<SmsSubmissionRecordMapper, SmsSubmissionRecord> implements ISmsSubmissionRecordService {

    @Autowired
    private SmsSubmissionRecordMapper smsSubmissionRecordMapper;


    @Autowired
    ISmsUserAllowOperatorService userAllowOperatorService;

    @Override
    public Result getSmsSubmissionRecordList(SmsSubmissionRecordDTO smsSubmissionRecordDTO) {
        IPage<SmsSubmissionRecord> page = new Page<>(smsSubmissionRecordDTO.getPageNum(), smsSubmissionRecordDTO.getPageSize());
        QueryWrapper<SmsSubmissionRecord> queryWrapper = new QueryWrapper<>();
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.HOUR, -48);
        Date start = calendar.getTime();
        start = Objects.isNull(smsSubmissionRecordDTO.getStime()) ?  start: smsSubmissionRecordDTO.getStime();
        Date end = Objects.isNull(smsSubmissionRecordDTO.getEtime()) ? now : smsSubmissionRecordDTO.getEtime();
        queryWrapper.le("submit_time", end).ge("submit_time", start).orderByDesc("submit_time");
        return ResultGenerator.genSuccessResult(this.page(page, queryWrapper));
    }

    @Override
    public Result getSmsDashBoardList(SmsSubmissionRecordDTO smsSubmissionRecordDTO) {
        if (smsSubmissionRecordDTO.getDuration().equals("week")) {
            smsSubmissionRecordDTO.setStime(DateUtils.getBeginDayOfWeek());
            smsSubmissionRecordDTO.setEtime(DateUtils.getEndDayOfWeek());
        } else if (smsSubmissionRecordDTO.getDuration().equals("month")) {
            smsSubmissionRecordDTO.setStime(DateUtils.getBeginDayOfMonth());
            smsSubmissionRecordDTO.setEtime(DateUtils.getEndDayOfMonth());
        } else if (smsSubmissionRecordDTO.getDuration().equals("quarter")) {
            smsSubmissionRecordDTO.setStime(DateUtils.getCurrentQuarterStartTime());
            smsSubmissionRecordDTO.setEtime(DateUtils.getCurrentQuarterEndTime());
        } else if (smsSubmissionRecordDTO.getDuration().equals("year")) {
            smsSubmissionRecordDTO.setStime(DateUtils.getBeginDayOfYear());
            smsSubmissionRecordDTO.setEtime(DateUtils.getEndDayOfYear());
        } else if (smsSubmissionRecordDTO.getDuration().equals("day")) {
            smsSubmissionRecordDTO.setStime(DateUtils.getDayBegin());
            smsSubmissionRecordDTO.setEtime(DateUtils.getDayEnd());
        }
        final Integer userId = smsSubmissionRecordDTO.getUserId();
        final Integer role = userAllowOperatorService.getRole(userId);
        if (Objects.equals(2, role)) {
            smsSubmissionRecordDTO.setRoleLevel(role);
            smsSubmissionRecordDTO.setUserIds(null);
        } else {
            Set<Integer> userIds = getUserIds(userId);
            smsSubmissionRecordDTO.setUserIds(userIds);
        }
        List<SmsDashBoardReportDTO> dbReport = smsSubmissionRecordMapper.getSmsDashBoardReport(smsSubmissionRecordDTO);
        SmsDashBoardReportDTO totalRecord = smsSubmissionRecordMapper.getTotalSmsDashBoardReport(smsSubmissionRecordDTO);
        SmsTotalDashBoardReportDTO smsTotalDashBoardReportDTO = new SmsTotalDashBoardReportDTO();
        smsTotalDashBoardReportDTO.setDashBoardReport(dbReport);
        smsTotalDashBoardReportDTO.setTotalDashBoardReport(totalRecord);
        return ResultGenerator.genSuccessResult(smsTotalDashBoardReportDTO);
    }

    @Autowired
    private SmsUserMapper smsUserMapper;


    public Set<Integer> getUserIds(Integer userId) {
        Set<Integer> idsList = new HashSet<>();
        //idsList.add(userId);
        Set<Integer> iddsList = getUserByParentId(userId, idsList);
        return iddsList;

    }

    private Set<Integer> getUserByParentId(Integer userId, Set<Integer> idsList) {
        idsList.add(userId);
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_user_id", userId);
        List<SmsUser> smsUsers = smsUserMapper.selectList(queryWrapper);
        for (SmsUser smsUser : smsUsers) {
            getUserByParentId(smsUser.getUserId(), idsList);
        }
        return idsList;
    }

    @Override
    public Result getSmsDashBoardListByCountryOrNetwork(SmsSubmissionRecordDTO smsSubmissionRecordDTO) {
        if (smsSubmissionRecordDTO.getDuration().equals("week")) {
            smsSubmissionRecordDTO.setStime(DateUtils.getBeginDayOfWeek());
            smsSubmissionRecordDTO.setEtime(DateUtils.getEndDayOfWeek());
        } else if (smsSubmissionRecordDTO.getDuration().equals("month")) {
            smsSubmissionRecordDTO.setStime(DateUtils.getBeginDayOfMonth());
            smsSubmissionRecordDTO.setEtime(DateUtils.getEndDayOfMonth());
        } else if (smsSubmissionRecordDTO.getDuration().equals("quarter")) {
            smsSubmissionRecordDTO.setStime(DateUtils.getCurrentQuarterStartTime());
            smsSubmissionRecordDTO.setEtime(DateUtils.getCurrentQuarterEndTime());
        } else if (smsSubmissionRecordDTO.getDuration().equals("year")) {
            smsSubmissionRecordDTO.setStime(DateUtils.getBeginDayOfYear());
            smsSubmissionRecordDTO.setEtime(DateUtils.getEndDayOfYear());
        } else if (smsSubmissionRecordDTO.getDuration().equals("day")) {
            smsSubmissionRecordDTO.setStime(DateUtils.getDayBegin());
            smsSubmissionRecordDTO.setEtime(DateUtils.getDayEnd());
        }


        final Integer userId = smsSubmissionRecordDTO.getUserId();
        final Integer role = userAllowOperatorService.getRole(userId);
        if (Objects.equals(2, role)) {
            smsSubmissionRecordDTO.setRoleLevel(role);
            smsSubmissionRecordDTO.setUserIds(null);
        } else {
            Set<Integer> userIds = getUserIds(userId);
            smsSubmissionRecordDTO.setUserIds(userIds);
        }
        List<SmsDashBoardReportDTO> dbReport = smsSubmissionRecordMapper.getSmsDashBoardReportByCountryOrNetwork(smsSubmissionRecordDTO);
        SmsDashBoardReportDTO totalRecord = smsSubmissionRecordMapper.getTotalSmsDashBoardReportByCountryOrNetwork(smsSubmissionRecordDTO);
        SmsTotalDashBoardReportDTO smsTotalDashBoardReportDTO = new SmsTotalDashBoardReportDTO();
        smsTotalDashBoardReportDTO.setDashBoardReport(dbReport);
        smsTotalDashBoardReportDTO.setTotalDashBoardReport(totalRecord);
        return ResultGenerator.genSuccessResult(smsTotalDashBoardReportDTO);
    }

}
